#include "appconfig.h"
#include <QHostInfo>

AppConfig* g_AppConfig = nullptr;

QString getLocalIPv4Addr()
{
    QHostInfo info = QHostInfo::fromName(QHostInfo::localHostName());
    //QHostAddress localAddress = info.addresses().first();
    QString localIpAddr = "";
    QList<QHostAddress>addrlist = info.addresses();
    if(!addrlist.isEmpty()){
        for(QHostAddress aHost:addrlist){
            if(QAbstractSocket::IPv4Protocol == aHost.protocol()){
                localIpAddr = aHost.toString();
                break;
            }
        }
    }
    qDebug() << "localIpAddr:" << localIpAddr;
    return localIpAddr;
}

AppConfig::AppConfig()
{
    InitConfig();
}

AppConfig::~AppConfig()
{

}

AppConfig *AppConfig::Instance()
{
    if (g_AppConfig == nullptr)
        g_AppConfig = new AppConfig;

    return g_AppConfig;
}

void AppConfig::InitConfig()
{
    //ToDo init config
    m_LocalIP = getLocalIPv4Addr();
    m_BroadCastIP = "255.255.255.255";//QHostAddress::Broadcast; //
    m_BroadCastPort = 6000;

    m_MutiCastIP="224.0.0.88";
    m_MutiCastPort = 8888;
    m_localPort_Muticast = 8888;

    m_TargetIP_UniCast="192.168.1.3";
    m_TargetPort_UniCast = 5000;
    m_LocalPort_UniCast = 5001;

    m_CastType = BroadCast_type;    


}

void AppConfig::SetBroadCastIP(const QString& ip)
{
    m_BroadCastIP = ip;
}

QString AppConfig::BroadCastIP()
{
    return m_BroadCastIP;
}

void AppConfig::SetBroadCastPort(unsigned int port)
{
    m_BroadCastPort = port;
}

unsigned int AppConfig::BroadCastPort()
{
    return m_BroadCastPort;
}



void AppConfig::SetLocalIP(QString& ip)
{
    m_LocalIP = ip;
}

QString AppConfig::LocalIP()
{
    return m_LocalIP;
}

const QString &AppConfig::MutiCastIP() const
{
    return m_MutiCastIP;
}

void AppConfig::setMutiCastIP(const QString &newMutiCastIP)
{
    m_MutiCastIP = newMutiCastIP;
}

int AppConfig::MutiCastPort() const
{
    return m_MutiCastPort;
}

void AppConfig::setMutiCastPort(int newMutiCastPort)
{
    m_MutiCastPort = newMutiCastPort;
}



int AppConfig::LocalPort_UniCast() const
{
    return m_LocalPort_UniCast;
}

void AppConfig::setLocalPort_UniCast(int newLocalPort_UniCast)
{
    m_LocalPort_UniCast = newLocalPort_UniCast;
}

enum CastType AppConfig::CastType() const
{
    return m_CastType;
}

void AppConfig::setCastType(enum CastType newCastType)
{
    m_CastType = newCastType;
}

int AppConfig::localPort_Muticast() const
{
    return m_localPort_Muticast;
}

void AppConfig::setLocalPort_Muticast(int newLocalPort_Muticast)
{
    m_localPort_Muticast = newLocalPort_Muticast;
}

const QString &AppConfig::TargetIP_UniCast() const
{
    return m_TargetIP_UniCast;
}

void AppConfig::setTargetIP_UniCast(const QString &newTargetIP_UniCast)
{
    m_TargetIP_UniCast = newTargetIP_UniCast;
}

int AppConfig::TargetPort_UniCast() const
{
    return m_TargetPort_UniCast;
}

void AppConfig::setTargetPort_UniCast(int newTargetPort_UniCast)
{
    m_TargetPort_UniCast = newTargetPort_UniCast;
}




